package littlecyberwar.tool;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;

import littlecyberwar.ui.*;

public class MouseObliqueHandler extends MouseStraightHandler {
  // Shape origSelectedRectangle;
 
  // a common superclass for different kind of mouse action to game command
  // i.e. move straight ahead
  // rotate
  // side step, etc.
  public MouseObliqueHandler() {
    super();
    id = H_OBLIQUE;
	unitSelectedText = "Drag mouse in forward 45' direction and release button to move unit.";
	noUnitSelectedText = "Click on a unit to select for front oblique movement.";

  }

  protected void dragSelected( Point newP ) {
    // attempt to use transform on point to see if we can change
    // coordinate to conform to that of the new user space,
    // then just translate from that
    //
	selectedTrans = ((SelectedElementContainer)selectedElements.get(0)).element.getTransform();

	try {
	  endDragP = selectedTrans.inverseTransform(newP, null);
	} catch ( Exception ex ) {
	  ex.printStackTrace();
	}
	// now figure out just the Y difference between the 2 points
	double diffY = endDragP.getY() - startDragP.getY();
	double diffX = endDragP.getX() - startDragP.getX();
	
	// for forward movement only
	if ( diffY < 0 ) {
		AffineTransform currTrans;
		Shape originalRec;
//		selectedArea = new Area();
	
		if ( diffX > 0 ) {
		  // diffX positive means moving to upper right
		  // make sure diffX <= -diffY
		  //
		  if ( diffX > -diffY ) {
			diffX = -diffY;
		  }

			for (int i=0; i < selectedElements.size(); i++ ){
				SelectedElementContainer selEl = (SelectedElementContainer)selectedElements.get(i);
				Element el = selEl.element;
				AffineTransform trans= el.getTransform();
				currTrans = (AffineTransform)trans.clone();
				currTrans.translate(diffX, diffY);
				originalRec = el.getRectangle();
				selEl.selectedRectangle = currTrans.createTransformedShape(originalRec);
				selEl.currTrans = currTrans;
			
//				Rectangle2D rec2update = selEl.selectedRectangle.getBounds2D();
//				updateSelectedAreaForRepaint(rec2update);
				
				DirtyAreaManager.addDirtyArea(selEl.selectedRectangle);
	
			}
			pixMoved = Math.sqrt(Math.pow(diffX,2) + Math.pow(diffY,2));

		} else {
		  // in this case moving to upper left
		  //
		  if ( -diffX > -diffY ) {
			diffX = diffY;
		  }
		  for (int i=0; i < selectedElements.size(); i++ ){
			  SelectedElementContainer selEl = (SelectedElementContainer)selectedElements.get(i);
			  Element el = selEl.element;
			  AffineTransform trans= el.getTransform();
			  currTrans = (AffineTransform)trans.clone();
			  currTrans.translate(diffX, diffY);
			  originalRec = el.getRectangle();
			  selEl.selectedRectangle = currTrans.createTransformedShape(originalRec);
			  selEl.currTrans = currTrans;
			
//			  Rectangle2D rec2update = selEl.selectedRectangle.getBounds2D();
//			  updateSelectedAreaForRepaint(rec2update);
			  
			  DirtyAreaManager.addDirtyArea(selEl.selectedRectangle);
			
		  }
		  pixMoved = Math.sqrt(Math.pow(diffX,2) + Math.pow(diffY,2));

		}
	}
	return;
  }
///////////////////////////////////////////////////////////////////////
//    try {
//      endDragP = selectedTrans.inverseTransform(newP, null);
//    } catch ( Exception ex ) {
//      ex.printStackTrace();
//    }
//
//    //System.out.println("newP x=" + newP.getX() + ", y=" + newP.getY() );
//
//    // now figure out just the Y difference between the 2 points
//    double diffY = endDragP.getY() - startDragP.getY();
//    double diffX = endDragP.getX() - startDragP.getX();
//
//    // for forward movement only
//    // make sure side movement is <= forward movement
//    //
//    if ( diffY < 0 ) {
//      if ( diffX > 0 ) {
//        // diffX positive means moving to upper right
//        // make sure diffX <= -diffY
//        //
//        if ( diffX > -diffY ) {
//          diffX = -diffY;
//        }
//        currTrans = (AffineTransform)selectedTrans.clone();
//        currTrans.translate(diffX, diffY);
//        selectedRectangle = currTrans.createTransformedShape(originalRec);
//        pixMoved = Math.sqrt(Math.pow(diffX,2) + Math.pow(diffY,2));
//
//      } else {
//        // in this case moving to upper left
//        //
//        if ( -diffX > -diffY ) {
//          diffX = diffY;
//        }
//        currTrans = (AffineTransform)selectedTrans.clone();
//        currTrans.translate(diffX, diffY);
//        selectedRectangle = currTrans.createTransformedShape(originalRec);
//        pixMoved = Math.sqrt(Math.pow(diffX,2) + Math.pow(diffY,2));
//      }
//    } else {
//      // otherwise no movement
//      currTrans = selectedTrans;
//      // selectedRectangle = origSelectedRectangle;
//      endDragP = startDragP;
//    }
//  }

}